﻿package igz.fleaxy.ui.calendar 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.DateTimeUtil;
	import igz.fleaxy.util.ObjectUtil;
	/**
	 * ...
	 * @author 
	 */
	public class CalendarDayUi extends Sprite {
		
		protected var _CalendarUi : CalendarUi;
		
		protected var _Bg : Shape;
		protected var _BgOver : Shape;
		protected var _Text : LabelUi;
		protected var _Style : String;
		protected var _StyleOver : String;
		
		protected var _Date_onClick : Function;
		protected var _Date_unSelect : Function;
		
		public var $Date : Date;
		
		protected var _Settings:Object = { "dayWidth" : 23
										  ,"dayHeight" : 23
										  ,"dayBg" : 0xFFFFFF
										  ,"bgAlpha" : 1
										  ,"dayBgWeekEnd" : 0xFAAC58
										  ,"dayBgColorOver" : 0xe34c26
										  ,"dayBgColorBorderOver" : 0xD3DBDE
										  ,"dayBgBorderOver" : 0
										  ,"dayLabelStyle" : "defaultTextDay"
										  ,"dayLabelStyleOver" : "defaultTextDay"
										  ,"dayLabelStyleToday" : "defaultTextToday"
										  ,"dayLabelStyleTodayOver" : "defaultTextToday"
										  ,"dayLabelStyleSpecial" : "defaultTextSpecial"
										  ,"dayLabelStyleSpecialOver" : "defaultTextSpecial"
										  ,"specialDate" : null // SpecialDate
										  ,"onSelectDay" : null // function ( p_date:Date ) {}
										  ,"unSelectDay" : null // function ( p_date:Date ) {}
										  , "onSelectSpecialDay" : null // function ( p_specialDate:SpecialDate ) {}
										};
										
		/**
		 * Componente Calendar Day
		 *
		 * @param	p_calendarUi   CalendarUi //Caledario en cual añadimos el dia
		 * @param	p_day Number Dia que vamos crear
		 * @param   p_settings = { "dayWidth" : 23 //Ancho de la caja del dia
										  ,"dayHeight" : 23 //Altura de la caja del dia
										  ,"dayBg" : 0xFFFFFF //Color de fondo de la caja
										  ,"bgAlpha" : 1 //Tranparencia de la caja de fondo 
										  ,"dayBgWeekEnd" : 0xFAAC58 //Color de los dias en fin de semana
										  ,"dayBgColorOver" : 0xe34c26 //Color de fondo de la caja
										  ,"dayAlphaOver" : 0 //Color //Tranparencia de la caja
										  ,"dayBgColorBorderOver" : 0xD3DBDE //Color del border
										  ,"dayBgBorderOver" : 0 //Ancho del liena de border
										  ,"dayLabelStyle" : "defaultTextDay" //Style de un dia normal
										  ,"dayLabelStyleOver" : "defaultTextDay" //Style de un dia normal
										  ,"dayLabelStyleToday" : "defaultTextToday" //Style de lo dia de hoy
										  ,"dayLabelStyleTodayOver" : "defaultTextToday" //Style de lo dia de hoy
										  ,"dayLabelStyleSpecial" : "defaultTextSpecial" //Style de los special day
										  ,"dayLabelStyleSpecialOver" : "defaultTextSpecial" //Style de los special day
										  ,"specialDate" : null // SpecialDate
										  ,"onSelectDay" : null // function ( p_date:Date ) {}
										  ,"unSelectDay" : null // function ( p_date:Date ) {}
										  , "onSelectSpecialDay" : null // function ( p_specialDate:SpecialDate ) {}
										};
		 *
		 * @example
		 *
		 * var date:Date = new date(2010,04,10);
		 * 
		 * var _Day:CalendarDayUi = new CalendarDayUi( this, date.getDate(), { "dayBgColorBorderOver" : 0xD3DBDE, "dayBgBorderOver" : 1 , "onSelectSpecialDay" : _onSelectSpecialDate} );
		 *
		 * addChild( _Day );
		 *
		 */
		
		public function get $Settings():Object {
			return _Settings;
		}
		
		public function CalendarDayUi( p_calendarUi : CalendarUi, p_day : Number, p_settings:Object = null ) {
			ObjectUtil.$Merge(p_settings, _Settings);
			
			
			

			_CalendarUi = p_calendarUi;
			
			if (_Settings["specialDate"] is SpecialDate) {
				if (_Settings["onSelectSpecialDay"] is Function) {
					_Date_onClick = _Settings["onSelectSpecialDay"];
				}
				if ((_Settings["specialDate"] as SpecialDate).$bgColor != 0) {
					_Settings["dayBg"] = (_Settings["specialDate"] as SpecialDate).$bgColor;
				}
			} else {
				if(_Settings["onSelectDay"] is Function) {
					_Date_onClick = _Settings["onSelectDay"];
				}
			}
			
			if (_Settings["unSelectDay"] is Function) {
				_Date_unSelect = _Settings["unSelectDay"];
			}

			// Data de dia creado
			$Date = new Date( _CalendarUi.$Date.fullYear, _CalendarUi.$Date.month, p_day );
			
			_Style = _Settings["dayLabelStyle"]
			if ( _Settings["dayLabelStyleOver"] == null) {
				_Settings["dayLabelStyleOver"] = _Settings["dayLabelStyle"];
			}
			
			var today:Date = new Date();
			if ( _DatesCompare($Date, today ) ) {
				_Style = _Settings["dayLabelStyleToday"];
				_StyleOver = _Settings["dayLabelStyleTodayOver"];
			}
			else if (_Settings["specialDate"] is SpecialDate && _Settings["dayLabelStyleSpecial"] != null) {
				_Style = _Settings["dayLabelStyleSpecial"];
					if (_Settings["dayLabelStyleSpecialOver"] != null) {
						_StyleOver = _Settings["dayLabelStyleSpecialOver"];
				}
				} else {
					_Style = _Settings["dayLabelStyle"];
					_StyleOver = _Settings["dayLabelStyleOver"];
				}
				
				if (($Date.day == 0 || $Date.day == 6) && _Settings["specialDate"] == null ) {
					_Settings["dayBg"] = _Settings["dayBgWeekEnd"];
				}
				
			_Bg = new Shape();
			addChild(_Bg);
				
			_BgOver = new Shape();
			addChild( _BgOver );
			
			_Text = new LabelUi( p_day.toString(), _Style );
			addChild( _Text );
			
			_Draw();
			
			buttonMode = true;
			mouseEnabled = true;
			useHandCursor = true;
			tabEnabled = false;
			
			
			addEventListener(MouseEvent.MOUSE_OVER, _onMouseOver );
			addEventListener(MouseEvent.MOUSE_OUT, _onMouseOut );
			addEventListener(MouseEvent.CLICK, _onMouseClick );
			
		}
		
		protected function _Draw() : void {
			_Bg.graphics.clear();
			_Bg.graphics.beginFill( _Settings["dayBg"], _Settings["bgAlpha"] );
			_Bg.graphics.drawRect( 1, 0, _Settings["dayWidth"] - 2, _Settings["dayHeight"]);
			_Bg.graphics.endFill();
			
			_BgOver.graphics.clear();
			_BgOver.graphics.lineStyle(_Settings["dayBgBorderOver"], _Settings["dayBgColorBorderOver"]);
			_BgOver.graphics.beginFill( _Settings["dayBgColorOver"], 1 );
			_BgOver.graphics.drawRect( 2 , 2, _Settings["dayWidth"] - 5, _Settings["dayWidth"] - 6 );
			_BgOver.graphics.endFill();
			_BgOver.alpha = 0;
			
			_Text.x = width - _Text.width >> 1;
			_Text.y = height - _Text.height >> 1;
		}
		
		protected function _onMouseOver( p_mouseEvent : MouseEvent) : void {
			_BgOver.alpha = 1;
		}
		
		protected function _onMouseOut( p_mouseEvent : MouseEvent) : void {
			if ( _CalendarUi.$Selected_CalendarDayUi != this ) {
				_BgOver.alpha = 0;
			}
		}
		
		public function $SetSpecial( p_specialDate : SpecialDate ) : void {
			
			_Text.$StyleName = _Style;
			_Draw();
			
			_BgOver.alpha = 1;
			
		}
		
		
		//public function _onBg() : void
		public function $Unselect() : void
		{
			_BgOver.alpha = 0;
			_Text.$StyleName = _Style;
		}
		
		//public function _outBg() : void
		public function $Select() : void
		{
			_BgOver.alpha = 1;
			_Text.$StyleName = _StyleOver;
			$getMetodo();
		}
		
		protected function _onMouseClick( p_mouseEvent : MouseEvent ) : void
		{
			if (_CalendarUi.$Selected_CalendarDayUi != this) {
				if (_Date_onClick != null) {
					if (_CalendarUi.$Selected_CalendarDayUi != null) {
						_CalendarUi.$Selected_CalendarDayUi.$Unselect();
					}
					_CalendarUi.$Selected_CalendarDayUi = this;
					_CalendarUi.$Fecha = DateTimeUtil.$Clone(this.$Date);
					$getMetodo();
					_Text.$StyleName = _StyleOver;
				}
				} else {
					_CalendarUi.$Selected_CalendarDayUi.$Unselect();
					_CalendarUi.$Fecha = null;
					_CalendarUi.$Selected_CalendarDayUi = null;
					if (_Date_unSelect != null) {
						_Date_unSelect($Date);
					}
			}
			
			
				
		}
		
		public function $getMetodo():void {
			if (_Date_onClick is Function) {
				if (_Settings["specialDate"] is SpecialDate) {
					_Date_onClick(_Settings["specialDate"]);
				} else {
					_Date_onClick($Date);
				}
			}
		}
		
		protected function _DatesCompare(_dateA:Date, _dateB:Date) :Boolean
		{
			if (_dateA.fullYear == _dateB.fullYear && _dateA.month == _dateB.month && _dateA.date == _dateB.date) {
				return true;
			}
			return false;
		}
		
		public function $setStyleDay(p_style:String):void {
			_Text.$StyleName = p_style;
		}
		
	}

}